package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.interfaces.OrderService;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.resources.date.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    //  t_ordersetting 表
    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Order add(Map map) {
        try {
            String name = (String) map.get("name");
            String sex = (String) map.get("sex");

            String setmeal_Id = (String) map.get("setmealId"); //套餐组ID
            int setmealId = Integer.parseInt(setmeal_Id);

            String telephone = (String) map.get("telephone");
            String idCard = (String) map.get("idCard");
            String orderDate = (String) map.get("orderDate");

            //   查询  t_ordersetting  根据日期判断当前是否已经预约满
            int count = orderSettingMapper.isOrderOk(orderDate);
            if (count > 0) {
                //  1.可以预约   :  t_order  生成一张订单
                Order order = new Order();
                //转换成日期类型
                Date date = DateUtils.parseString2Date(orderDate);
                order.setOrderStatus(Order.ORDERSTATUS_NO);//未到诊
                order.setOrderDate(date);
                order.setOrderType(Order.ORDERTYPE_WEIXIN);//微信预约
                order.setSetmealId(setmealId);
                //   2.   预约表  已经预约数 +1   update  xx  set  reservations = reservations+1 where orderDate = ?
                orderSettingMapper.updateReservationsByOrderDate(orderDate);
                //  3.   根据手机号 查询会员 是否存在 ， 如果不存在  insert 系统
                Member member = memberMapper.findMemeberBytelephone(telephone);
                if (member == null) {
                    member = new Member();
                    member.setName(name);
                    member.setRegTime(new Date());
                    member.setIdCard(idCard);
                    member.setSex(sex);
                    member.setPhoneNumber(telephone);
                    memberMapper.insert(member);//插入
                } else {
                    //  4. 老会员    getId()     老会员有可能该套餐 当天已经预约过了！
                    int orderCount = baseMapper.findOrderIsExistByOrderDateAndSetmealIdAndMemberId(orderDate, setmealId, member.getId());
                    if (orderCount > 0) {
                        //  老会员 已经预约过了
                        throw new RuntimeException("请勿重复预约！");
                    }
                }
                order.setMemberId(member.getId());//会员id
                baseMapper.insert(order);
                return order;
            } else {
                //  预约满了
                throw new RuntimeException("当天预约满了，请改期");
            }
        } catch (Exception e) {
            e.printStackTrace();
            //返回异常详细信息
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public Map findSetMealDetail(Integer orderid) {

        return baseMapper.findOrderInfoByOrderId(orderid);
    }
}
